package cn.haut.online_read.mapper;

import cn.haut.online_read.enrity.BookInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author xing
 * @date 2021-05-14 19:07
 */
@Mapper
public interface BookshelfMapper {

    /**
     * 查找书信息 通过书的id
     * @param id
     * @return
     */
    @Select("SELECT * FROM book_info WHERE id = #{id}")
    BookInfo getBookInfoById(String id);

    /**
     * 添加新的书籍信息
     * @param bookInfo
     * @return
     */
    @Insert("INSERT INTO book_info (id, name, imgUrl) VALUES (#{id}, #{name}, #{imgUrl})")
    Integer addBookInfo(BookInfo bookInfo);

    @Insert("INSERT INTO bookcase (book_id,user_id) VALUES (#{bookId}, #{username})")
    Integer addShelf(String username, String bookId);

    @Select("SELECT COUNT(*) FROM bookcase WHERE user_id = #{username} AND book_id = #{bookId}")
    Integer getCountByAll(String username, String bookId);

    @Select("SELECT COUNT(*) FROM book_info WHERE id = #{id}")
    Integer getInfoCountById(String id);

    /**
     * 获取用户书架中的书籍信息
     *
     * @param username
     * @return
     */
    @Select("SELECT book_info.id, book_info.name, book_info.imgUrl " +
            "FROM book_info, bookcase " +
            "WHERE bookcase.user_id = #{username} " +
            "AND book_info.id = bookcase.book_id")
    List<Map<String, String>> getBooks(String username);

    /**
     * 删除 bookcase 表中 user_id 与 book_id 符合条件的项
     * @param username 用户名
     * @param bookId 书籍 id
     * @return
     */
    @Delete("DELETE FROM bookcase WHERE user_id = #{username} AND book_id = #{bookId}")
    Boolean removeBook(String username, String bookId);
}
